package com.caifu.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caifu.pojo.SysLog;
import com.caifu.pojo.SysUser;
import com.caifu.service.SysUserService;
import com.caifu.util.AesMyUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aliyuncs.http.HttpRequest;
import com.caifu.bean.Result;
import com.caifu.bean.ResultUtil;
import com.caifu.pojo.SysShortMessage;
import com.caifu.service.SysShortMessageService;
import com.caifu.util.Util;

import net.sf.json.JSONObject;

/**
 * 短信发送管理
 * 
 * @author szt
 * @date 2020年8月14日
 */
@Controller
@RequestMapping("/msg")
public class ShortMessageController extends AbstractController{
	private static final Logger logger = LoggerFactory.getLogger(ShortMessageController.class);
	 @Autowired
	 private SysShortMessageService msgService;
	 @Autowired
	private SysUserService userService;
	 
	 /**
     * 发送验证码
     * 192.168.1.180:9999/caifu/msg/sendRandomCode?phone=15801895528
     * 2020年8月14日 
     * codeBy szt
     */
    @RequestMapping("/sendRandomCode")
    @ResponseBody
    public Result sendRandomCode(String phone, HttpServletRequest request){
        try{
        	//校验手机号是否存在
			int phoneExistCount = userService.count(new QueryWrapper<SysUser>().eq("phone", AesMyUtil.Encrypt(phone)).eq("post_type","1"));
			if(phoneExistCount==0) {
				return ResultUtil.error(199,"手机号不存在！");
			}

        	String random = Util.getRandom(4);   //4位手机验证码
			//System.out.println(random);
        	//发送短信
			JSONObject json = new JSONObject();
			json.put("code", random);
			JSONObject jsonRet = msgService.sendShortMessage("智慧社区", phone, "SMS_198875209", json);
			if(!jsonRet.get("code").equals("0000")){
				return ResultUtil.error(199,"发送短信失败！");
			}
			
			//session保存手机号和验证码
			HttpSession session = request.getSession();
            session.setAttribute("verifyCode",random);
            session.setAttribute("verifyPhone",phone);
            
            //保存发短信日志
        	String msgContent="验证身份，验证码为："+random;
            SysShortMessage msg = new SysShortMessage(phone, msgContent, new Date());
            msgService.save(msg);
           
			System.out.println(random);
        	return ResultUtil.success(200, "发送成功");
        }catch (Exception e){
            logger.error("发送短信失败！");
            e.printStackTrace();
            return ResultUtil.error(199,"发送短信失败！");
        }
    }

	/**
	 * 校验手机号验证码
	 * 2020年11月19日
	 * codeBy szt
	 */
	@RequestMapping("/checkPhone")
	@ResponseBody
	public Result checkPhone(String phone, String verifyCode , HttpServletRequest request){
		try{
			JSONObject jsonRet = msgService.checkVerifyCode(phone, verifyCode, request);
			int phoneExistCount = userService.count(new QueryWrapper<SysUser>().eq("phone", AesMyUtil.Encrypt(phone)).eq("post_type","1"));
			if(phoneExistCount==0) {
				return ResultUtil.error(199,"手机号不存在！");
			}else{
				if(!jsonRet.get("code").equals("0000")){
					return ResultUtil.error(199,(String)jsonRet.get("codeDesc"));
				}else {
					return ResultUtil.success(200,(String)jsonRet.get("codeDesc"), AesMyUtil.Encrypt(phone));
				}
			}

		}catch (Exception e){
			logger.error("校验验证码失败！");
			return ResultUtil.error(199,"校验验证码失败！");
		}
	}
}
